package com.ophiux.cs.application.consultBase.impl;

import com.ophiux.cs.application.consultBase.dto.GetCSDoctorInfoByIDRequest;
import com.ophiux.cs.application.consultBase.dto.GetCSDoctorInfoByIDResponse;
import com.ophiux.cs.application.consultBase.dto.GetCSDoctorsRequest;
import com.ophiux.cs.application.consultBase.dto.GetCSDoctorsResponse;
import com.ophiux.cs.application.consultBase.service.IGetCSDoctorsAppService;
import com.ophiux.cs.application.core.AppResultCode;
import com.ophiux.cs.application.core.ResponsePage;
import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.ResultEnum;
import com.ophiux.cs.domain.consultBase.model.valueobj.CSDoctorsResult;
import com.ophiux.cs.domain.consultBase.model.valueobj.CSTypeResult;
import com.ophiux.cs.domain.consultBase.model.valueobj.DoctorQueryObj;
import com.ophiux.cs.domain.consultBase.repositories.IDoctorRepository;
import com.ophiux.utils.common.ShiftUtils;
import com.ophiux.utils.exception.OphiuxRepositoryException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class GetCSDoctorsAppService implements IGetCSDoctorsAppService {

    @Autowired
    IDoctorRepository doctorRepositories;


    @SuppressWarnings("unchecked")
	@Override
    public Result<ResponsePage<GetCSDoctorsResponse>> getCSDoctors(GetCSDoctorsRequest dto) {
        ResponsePage<GetCSDoctorsResponse> responsePage=new ResponsePage<GetCSDoctorsResponse>();
        List<GetCSDoctorsResponse> returnList = new ArrayList<GetCSDoctorsResponse>();
        //验证参数是否允许为空
        if(!dto.isParamValid()) {
            return Result.fail(ResultEnum.requestParamFail.getCode(), ResultEnum.requestParamFail.getMsg());
        }
        try {
            DoctorQueryObj doc = null;
            doc = (DoctorQueryObj) ShiftUtils.objToNewObj(dto, DoctorQueryObj.class);
            try {
                List<CSDoctorsResult> docList=doctorRepositories.getCSDoctors(doc);
                //获取当前医生的咨询列表
                for(int i=0;i<docList.size();i++){
                    CSDoctorsResult ds=docList.get(i);
                    //根据医生ID查询咨询方式
                    List<CSTypeResult> csTypeList=doctorRepositories.getCSTypeByDoctorId(ds.getDOCTOR_ID());
                    ds.setCS_TYPES(csTypeList);
                }
               int count=doctorRepositories.getCSDoctorsCount(doc);
                returnList = ShiftUtils.objToNewObjList(docList, GetCSDoctorsResponse.class);
                responsePage.initData(returnList,count,doc);
            } catch (OphiuxRepositoryException e) {
                e.printStackTrace();
                return Result.fail(ResultEnum.requestException.getCode(), e.getExceptionMessage());
            }
        } catch (Exception e) {
            log.info("getCSDoctors接口异常：",e);
            e.printStackTrace();
            return Result.fail(AppResultCode.EXCEPTION, "程序异常");
        }
        return Result.success(responsePage, "查询咨询医生成功");
    }

    @Override
    public Result<GetCSDoctorInfoByIDResponse> GetCSDoctorInfoByID(GetCSDoctorInfoByIDRequest dto) {
        GetCSDoctorInfoByIDResponse resp=new GetCSDoctorInfoByIDResponse();
        //验证参数是否允许为空
        if(!dto.isParamValid()) {
            return Result.fail(ResultEnum.requestParamFail.getCode(), ResultEnum.requestParamFail.getMsg());
        }
        try {
            DoctorQueryObj doc = null;
            doc = (DoctorQueryObj) ShiftUtils.objToNewObj(dto, DoctorQueryObj.class);
            try {
                List<CSDoctorsResult> docList=doctorRepositories.getCSDoctors(doc);
                //获取当前医生的咨询列表
                for(int i=0;i<docList.size();i++){
                    CSDoctorsResult ds=docList.get(i);
                    //根据医生ID查询咨询方式
                    List<CSTypeResult> csTypeList=doctorRepositories.getCSTypeByDoctorId(ds.getDOCTOR_ID());
                    ds.setCS_TYPES(csTypeList);
                }
                if(docList!=null && docList.size()!=0){
                    resp= (GetCSDoctorInfoByIDResponse) ShiftUtils.objToNewObj(docList.get(0), GetCSDoctorInfoByIDResponse.class);
                 }else{
                    resp=null;
                }

             } catch (OphiuxRepositoryException e) {
                e.printStackTrace();
                return Result.fail(ResultEnum.requestException.getCode(), e.getExceptionMessage());
            }
        } catch (Exception e) {
            log.info("getCSDoctors接口异常：",e);
            e.printStackTrace();
            return Result.fail(AppResultCode.EXCEPTION, "程序异常");
        }
        return Result.success(resp, "查询咨询医生成功");

    }
}
